package AccountWindow.SystemAdmin.NewRegisterWindow;

import AccountWindow.SystemAdmin.Account;
import AccountWindow.SystemAdmin.NewRegisterWindow.Dao.NewRegisterDAO;
import SignAndRegister.RemindWindow;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

public class NewRegisterWindow extends JPanel{

    private Vector<Vector> data;
    private JTable jTable;
    private JScrollPane jScrollPane;
    public JPanel newRegisterPanel;
    public NewRegisterWindow(String nowAccountID){
        try {
            newRegisterPanel = new JPanel();
            this.add(newRegisterPanel);
            BorderLayout borderLayout = new BorderLayout();
            newRegisterPanel.setLayout(borderLayout);

            JButton refreshButton = new JButton("刷新");
            refreshButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    try {
                        System.out.println("执行刷新命令");
                        refreshNewRegisterPanel();
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                }
            });

            jTable = getTable();
            jScrollPane = new JScrollPane(jTable);
            newRegisterPanel.add(refreshButton,BorderLayout.SOUTH);
            newRegisterPanel.add(jScrollPane,BorderLayout.CENTER);

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private DefaultTableModel defaultTableModel = new DefaultTableModel();  //为了显示JTable组件需要用到的一个类，该类实现了TableModel接口
    private Vector<String> vector;
    private boolean noFirstFlag = false;

    private JTable getTable() throws Exception{

        Vector headers = new Vector<>();
        headers.add("临时用户ID");
        headers.add("用户类型");
        headers.add("用户密码");
        headers.add("通过");
        headers.add("拒绝");

        if(noFirstFlag){
            data.clear();   //将data内所有数据清空
        }
        noFirstFlag = true;

        data = new NewRegisterDAO().query();
        JTable jTable = new JTable(data,headers);

        jTable.getColumnModel().getColumn(3).setCellRenderer(new TableCellRendererButtonOfPass());   //绘制名为“通过”的第3列的值
        jTable.getColumnModel().getColumn(3).setCellEditor(new TableCellEditorButtonOfPass());       //值编辑器和监听器
        jTable.getColumnModel().getColumn(4).setCellRenderer(new TableCellRendererButtonOfNoPass());
        jTable.getColumnModel().getColumn(4).setCellEditor(new TableCellEditorButtonOfNoPass());

        return jTable;

    }

    public void refreshNewRegisterPanel() throws Exception {
        newRegisterPanel.remove(jScrollPane);
        jTable = getTable();
        jScrollPane = new JScrollPane(jTable);
        newRegisterPanel.add(jScrollPane,BorderLayout.CENTER);
        newRegisterPanel.updateUI();
        newRegisterPanel.invalidate();
        newRegisterPanel.validate();
        newRegisterPanel.repaint();
    }

//--------------TableCellEditorButtonOfNoPass专场-----------------------------------------------------------

    private class TableCellEditorButtonOfNoPass extends DefaultCellEditor{
        private JButton jButton;

        public TableCellEditorButtonOfNoPass() {
            super(new JTextField());    //使用父类的构造方法
            this.setClickCountToStart(1);   //编辑点击一次即可激活操作
            jButton = new JButton("拒绝");
            jButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("系统管理员拒绝了该条申请");
                }
            });
        }

        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {     //重写
            String deleteAccountID = (String) table.getValueAt(row,0);
            String deleteAccouuntType = (String) table.getValueAt(row,1);
            String deleteAccouuntCode = (String) table.getValueAt(row,2);
            Account deleteAccount = new Account(deleteAccountID,deleteAccouuntType,deleteAccouuntCode);
            try {
                new NewRegisterDAO().remove(deleteAccount);
                refreshNewRegisterPanel();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return jButton;
        }
    }

//-------------TableCellEditorButtonOfPass专场--------------------------------------------------------------------------

    private class TableCellEditorButtonOfPass extends DefaultCellEditor {
        //该类为值编辑器，当用户对表格内的组件有动作的时候，有该类对事件做出反应
        private JButton jButton;
        public TableCellEditorButtonOfPass() {
            super(new JTextField());    //使用父类的构造方法
            this.setClickCountToStart(1);   //编辑点击一次即可激活操作
            this.jButton = new JButton("通过");
            jButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("系统管理员通过了该条申请");
                }
            });
        }

        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column){     //在这里定义被点击后发送的事件
            String insertAccountID = (String) table.getValueAt(row,0);
            String insertAccountType = (String) table.getValueAt(row,1);
            String insertAccountCode = (String) table.getValueAt(row,2);
            Account insertAccount = new Account(insertAccountID,insertAccountType,insertAccountCode);
            try {
                int isAddSuccess = new NewRegisterDAO().add(insertAccount);
                if(isAddSuccess==1){
                    new NewRegisterDAO().remove(insertAccount);
                }else{
                    //弹出弹窗提醒系统管理员，该ID已被注册，请主动取消
                    //其实更加方便的作法是在注册界面的时候就对account表和accountTmp表作双查询，这样就是万无一失的
                    //但我懒，不想做，所以我不做了，哈哈哈哈，其实是职工无法对已存在Tmp表中的ID再做插入，所以不做了，只设计一个安全保障
                    new RemindWindow("RegisterPleaseCancel");
                }
                refreshNewRegisterPanel();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return jButton;
        }
    }

}